package com.xaicode.template.gendata.mall;

import com.xaicode.template.gendata.mall.config.AppConfig;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Locker xaicode@sina.com
 * @version 1.0
 * @date 2022/5/24 0024 22:21
 */
@Component
public class MockTask {

    @Resource
    private ThreadPoolTaskExecutor poolExecutor;

    public void mainTask() {
        mainTask(AppConfig.mock_count);
    }

    public void mainTask(int count) {
        for (int i = 0; i < count; i++) {
            this.poolExecutor.execute(new Mocker());
        }

        while (true) {
            try {
                Thread.sleep(1000L);
                if (this.poolExecutor.getActiveCount() == 0) {
                    this.poolExecutor.destroy();
                    System.out.println("main task over ok");
                    return;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
